package org.accp.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.util.StringUtil;
import org.accp.dao.RegisterDao;
import org.accp.dao.RegisterTypeDao;
import org.accp.pojo.Complaint;
import org.accp.pojo.Hisrole;
import org.accp.pojo.Register;
import org.accp.pojo.RegisterType;
import org.accp.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.persistence.criteria.CriteriaBuilder;
import javax.xml.ws.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/register")
@CrossOrigin("*")
public class RegisterController {
    @Autowired
    private RegisterDao registerDao;
    @Autowired
    private RegisterTypeDao registerTypeDao;

    public void setRegisterDao(RegisterDao registerDao) {
        this.registerDao = registerDao;
    }
    @RequestMapping("/search")
    @ResponseBody
    public List<Register> search() throws Exception{
        return registerDao.getAll();
    }
    @RequestMapping("/list")
    @ResponseBody
    public ResponseResult<List<Register>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Register register,String registerTs) throws Exception {
        Map<String, Object> map = new HashMap<>();


//        if (StringUtil.isNotEmpty(registerTs)) {
//            String[] args = registerTs.split(",");
//            List<RegisterType> registerTypes = new ArrayList<>();
//            for (String str : args) {
//                RegisterType registerTypes1 = new RegisterType();
//                registerTypes1.setTypeid(Integer.parseInt(str));
//                registerTypes.add(registerTypes1);
//            }
//            register.setRegisterTypes(registerTypes);
//        }
            Page<Register> pager = PageHelper.startPage(page, limit);
            List<Register> list = registerDao.getConditionList(register);
            return new ResponseResult(pager.getTotal(), list);
        }
    @RequestMapping("/lists")
    @ResponseBody
    public ResponseResult<List<Register>> lists(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Register register,String registerTs) throws Exception {
        Map<String, Object> map = new HashMap<>();


//        if (StringUtil.isNotEmpty(registerTs)) {
//            String[] args = registerTs.split(",");
//            List<RegisterType> registerTypes = new ArrayList<>();
//            for (String str : args) {
//                RegisterType registerTypes1 = new RegisterType();
//                registerTypes1.setTypeid(Integer.parseInt(str));
//                registerTypes.add(registerTypes1);
//            }
//            register.setRegisterTypes(registerTypes);
//        }
        Page<Register> pager = PageHelper.startPage(page, limit);
        List<Register> lists = registerDao.getConditionLists(register);
        return new ResponseResult(pager.getTotal(), lists);
    }
    @RequestMapping("/listss")
    @ResponseBody
    public ResponseResult<List<Register>> listss(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, Register register,String registerTs) throws Exception {
        Map<String, Object> map = new HashMap<>();


//        if (StringUtil.isNotEmpty(registerTs)) {
//            String[] args = registerTs.split(",");
//            List<RegisterType> registerTypes = new ArrayList<>();
//            for (String str : args) {
//                RegisterType registerTypes1 = new RegisterType();
//                registerTypes1.setTypeid(Integer.parseInt(str));
//                registerTypes.add(registerTypes1);
//            }
//            register.setRegisterTypes(registerTypes);
//        }
        Page<Register> pager = PageHelper.startPage(page, limit);
        List<Register> listss = registerDao.getConditionListss(register);
        return new ResponseResult(pager.getTotal(), listss);
    }


    @RequestMapping("/batchDelete")
    @ResponseBody
    public ResponseResult batchDelete(Integer[] id) throws Exception{
        registerDao.batchDelete(id);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(int id) throws Exception{
        registerDao.delete(id);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(Register register) throws Exception{
        registerDao.add(register);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(Integer id) throws Exception{
        registerDao.xiugai(id);
        return ResponseResult.createSuccessResult();
    }

    @RequestMapping("/updates")
    @ResponseBody
    public ResponseResult updates(Integer id) throws Exception{
        registerDao.gai(id);
        return ResponseResult.createSuccessResult();
    }
    @RequestMapping("/updatess")
    @ResponseBody
    public ResponseResult updatess(Register register) throws Exception{
        registerDao.updatess(register);
        return ResponseResult.createSuccessResult();
    }
}
